﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace WsValid
{
    public sealed class IsEmailAttribute : ValidationAttribute, IClientValidatable
    {
        string reg = @"/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";

        public IsEmailAttribute()
        {
            ErrorMessage = "邮箱格式验证失败！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "isemail",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = (string)value;
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }

    public sealed class IsZipCodeAttribute : ValidationAttribute, IClientValidatable
    {

        string reg = @"/^\d{6,6}$/";
        public IsZipCodeAttribute()
        {
            ErrorMessage = "邮编格式验证失败！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "iszipcode",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = (string)value;
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }

    public sealed class IsPhoneAttribute : ValidationAttribute, IClientValidatable
    {
        string reg = @"/^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/";

        public IsPhoneAttribute()
        {
            ErrorMessage = "电话号码验证失败！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "isphone",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = (string)value;
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }

    public sealed class IsIDNoAttribute : ValidationAttribute, IClientValidatable
    {
        string reg = @"/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/";

        public IsIDNoAttribute()
        {
            ErrorMessage = "身份证格式错误！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "isidno",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = value.ToString();
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }

    public sealed class IsIntAttribute : ValidationAttribute, IClientValidatable
    {
        string reg = @"/^[0-9]*$/";

        public IsIntAttribute()
        {
            ErrorMessage = "请输入整数！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "isint",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = value.ToString();
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }

    public sealed class IsChineseAttribute : ValidationAttribute, IClientValidatable
    {
        string reg = @"/^[\u0391-\uFFE5]+$/";

        public IsChineseAttribute()
        {
            ErrorMessage = "请输入中文！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "isch",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = value.ToString();
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }

    public sealed class IsDoubleAttribute : ValidationAttribute, IClientValidatable
    {
        string reg = @"/^(-?\d+)(\.\d+)?$/";

        public IsDoubleAttribute()
        {
            ErrorMessage = "请输入数字！";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule rule = new ModelClientValidationRule
            {
                ValidationType = "isdouble",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };
            rule.ValidationParameters["inputstring"] = reg;
            yield return rule;
        }

        public override bool IsValid(object value)
        {
            string validExpression = reg;
            if (value == null)
                return true;
            string inputString = value.ToString();
            if (Regex.IsMatch(inputString, validExpression))
            {
                return false;
            }
            return true;
        }
    }
}
